var express = require("express");
var router = express.Router();
var db = require("../../model/db");
//首页数据渲染筛选
router.get("/show", async (req, res, next) => {
  let id = req.query.id
  console.log(id);
  const data = await db.query("select * from eyeglasses_shop");
  if (id) {
    //从数据库中找到对应id的数据
    const [data] = await db.query("select * from eyeglasses_shop where id=?", [id]);
    console.log(data);
    res.send({
      msg: "获取数据成功",
      data
    })
  } else {
    res.send({
      msg: "获取数据成功",
      data
    });
  }
});
router.get('/shop', async (req, res, next) => {
  try {
    const [data] = await db.query("SELECT * FROM eyeglasses_shop");
    console.log(data);
    res.send({
      code: 200,
      data

    })
  } catch (err) {
    console.log(err);
    res.send({
      code: 500,
    })
  }
})


router.get('/cate', async (req, res, next) => {
  try {
    const [data] = await db.query("SELECT * FROM eyeglasses_cate");
    res.send({
      code: 200,
      list:data
    })
  } catch (err) {
    console.log(err);
    res.send({
      code: 500,
    })
  }
})
router.get('/shop_cate', async (req, res, next) => {
  try {
    const data = await db.query(`
      SELECT 
        shop.id,
        shop.name, 
        shop.price, 
        shop.old_price,
        shop.video,
        shop.img, 
        shop.total,
        shop.create_time,
        shop.update_time,
        cate.cid, 
        cate.cate_name, 
        cate.level, 
        cate.parent_id,  
        shop_cate.cate_id
      FROM 
        eyeglasses_shop shop
      RIGHT JOIN 
        eyeglasses_shop_cate shop_cate ON shop.id = shop_cate.shop_id
      INNER JOIN 
        eyeglasses_cate cate ON shop_cate.cate_id = cate.cid
    `);
    console.log(data[0]);
    res.send({
      code: 200,
      list: data[0]

    })
  } catch (err) {
    console.log(err);
    res.send({
      code: 500,
    })
  }
})

//获取购物车数据
router.get("/cart", async (req, res) => {
  const carts = await db.query("SELECT * FROM eyeglasses_cart");
  res.send({
    code: 0,
    msg: "获取数据成功",
    carts
  });
});

//修改购物车数据状态
router.put('/upcart', async (req, res) => {
  let { id, status } = req.body
  await db.query(`update eyeglasses_cart set status = ${status} where id = ${id}`)
})

//全选
router.put('/upcartall', async (req, res) => {
  let { id, status } = req.body
  await db.query(`update eyeglasses_cart set status = ${status} where id in (${id.join()})`)
})

//清空
router.delete('/delcartall', async (req, res) => {
  await db.query('truncate table eyeglasses_cart')
})

//商品数量加/减
router.put('/cartlen', async (req, res) => {
  let { id, count } = req.body
  await db.query(`update eyeglasses_cart set count = ${count} where id = ${id}`)
  res.send({
    code: 200
  })
})

//添加订单列表
router.post('/orderadd',(req,res)=>{
  let {name,img,count} = req.body
  db.query('INSERT INTO eyeglasses_cart_order (name, img, count) VALUES (?, ?, ?)', [name, img, count])
  res.send('ok')
})

//获取订单列表数据
router.get('/orderlist',async(req,res)=>{
  let orders = await db.query("SELECT * FROM eyeglasses_cart_order");
  res.send({orders})
})

module.exports = router;




